﻿@{
    Layout = "~/Views/Shared/_PageLayout.cshtml";
}

@model Beway.ValueAdded.Model.Supplier
@section HeadSection
{
    <script type="text/javascript">
        var role = "@Session["role"]";
        var url = "";
        $(function () {
            $('#SuppList').datagrid({
                title: '供应商管理',
                iconCls: 'icon-edit',
                url: '/Supplier/SuppList',
                striped: true,
                idField: 'SuppId',
                border: true,
                loadMsg: '数据加载中...',
                singleSelect: true,
                pagination: true,
                pageSize: 10,
                pageList: [10, 20],
                frozenColumns: [[{ field: 'ck', checkbox: true }]],
                columns: [[
                    { field: 'SuppId', title: 'ID', width: 50, align: 'center' },
                        { field: 'SuppName', title: '供应商名称', width: 200 },
                        { field: 'Contact', title: '联系人', width: 80, align: 'center' },
                        { field: 'Mobile', title: '手机号', width: 90, align: 'center' },
                        { field: 'SuppApi',  hidden: true },
                         { field: 'SuppAccount', hidden: true },
                         { field: 'SuppPwd', hidden: true },
                         { field: 'SuppToken', hidden: true },
                         {
                             field: 'SuppStatus', title: '供应商状态', width: 100, align: 'center', formatter: function (val, rec) {
                                 if (val == 1) {
                                     return "<font style='color:green;'>启用</font>";
                                 }
                                 else {
                                     return "<font style='color:red;'>禁用</font>";
                                 }
                             }
                         },
                        {
                            field: 'AddTime', title: '添加时间', width: 120, align: 'center', formatter: function (val, rec) {
                                return JSONDateToJSDate(val);
                            }
                        } 
                ]], toolbar: [{
                    text: '新增',
                    iconCls: 'icon-add',
                    handler: function () {
                        add();
                    }
                }, '-', {
                    text: '编辑',
                    iconCls: 'icon-edit',
                    handler: function () {
                        edit();
                    }
                }, '-', {
                    text: '删除',
                    iconCls: 'icon-remove',
                    handler: function () {
                        del();
                    }
                }, '-']

            });

            //尺寸
            $('#SuppList').datagrid('resize');

            //加载搜索框//在这个样式下。加载搜索框的div
            $(".datagrid-toolbar").append($("#divsearch")); 
        });

        //搜索
        function doSearch() {
            $('#SuppList').datagrid('load', { UserName: $("#UserName").val() });
        }

        //新增
        function add() {
            if (role == 0) {
                $("#DivUserName").show();
                url = "/Supplier/add";
                $("#dlg").dialog('open').dialog('setTitle', '添加供应商');
                $("#fm").form('clear');
            }
            else
            {
                alert("抱歉，项目经理不能新增供应商。");
            }
        }
        //新增确定
        function save() {
            $.messager.progress();
            $("#fm").form('submit', {
                url: url,
                onSubmit: function () {

                    if (!$(this).form('validate')) {
                        $.messager.progress('close');
                        return false;
                    }
                },
                success: function (result) {
                    $.messager.progress('close');
                    if (result > 0) {
                        $("#dlg").dialog('close');
                        $.messager.alert('提示', '信息保存成功');
                        $("#SuppList").datagrid('reload');
                    } else   {
                        $.messager.show({
                            title: 'Error',
                            msg: "该供应商已经存在"
                        }
                        );
                    }                    
                }
            });
        }
        //编辑
        function edit() {
            var row = $("#SuppList").datagrid('getSelected');
            if (row) {
                $("#dlg").dialog('open').dialog('setTitle', '编辑供应商');           
                $("#fm").form('load', row);
                url = "/Supplier/Edit";

                $(".editTip").text("不修改请留空");

                if (row.SuppStatus == 0) //禁用
                {
                    $("#radio1").removeAttr("checked");
                    $("#radio2").attr("checked", "checked");
                }
                else {
                    $("#radio2").removeAttr("checked");
                    $("#radio1").attr("checked", "checked");
                }

            } else {

                $.messager.alert('提示', '请选择数据行进行编辑!');
            }
        }
        function del() {
            var row = $("#SuppList").datagrid('getSelected');
            if (row) {
                $.messager.confirm('Confirm', '您确定要删除该数据吗?', function (r) {
                    if (r) {
                        $.post('/supplider/del', { id: row.UserID }, function (result) {
                            if (result == "ok") {
                                $("#SuppList").datagrid('reload');
                            }
                            else {
                                $.messager.alert('提示', result);
                            }
                        });
                    }
                });
            } else {
                $.messager.alert('提示', '请选择数据行进行操作!');
            }
        }
    </script>
}
<div id="divsearch">
    供应商名称：<input type="text" name="UserName" id="UserName" style="width: 80px" />
    <a href="#" class="easyui-linkbutton" onclick="doSearch();" iconcls="icon-search">搜索</a>
</div>
<table id="SuppList" toolbar="#divsearch"></table>
<div id="dlg-buttons">
    <a href="#" class="easyui-linkbutton" iconcls="icon-ok" onclick="save()">保存</a>
    <a href="#" class="easyui-linkbutton" iconcls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">取消</a>
</div>
<div id="dlg" class="easyui-dialog" style="width: 800px; padding: 10px 10px" resizable="true"
     closed="true" modal="true" buttons="#dlg-buttons">
    <form id="fm" method="post" enctype="multipart/form-data">
        <div class="fitem">
            <label>供应商名称：</label>
            @Html.TextBoxFor(m => m.SuppName, new { @class = "easyui-validatebox", @style = "width:200px;", @required = "true" })
        </div>
        <div class="fitem">
            <label>供应商API地址：</label>
            @Html.TextBoxFor(m => m.SuppApi, new { @class = "easyui-validatebox", @style = "width:200px;", @maxlength = "200" }) <span class="editTip"></span>
        </div>
        <div class="fitem">
            <label>供应商账号：</label>
            @Html.TextBoxFor(m => m.SuppAccount, new { @class = "easyui-validatebox", @style = "width:250px;",  @maxlength = "50" }) <span class="editTip"></span>
        </div>
        <div class="fitem">
            <label>供应商密码：</label>
            @Html.TextBoxFor(m => m.SuppPwd, new { @class = "easyui-validatebox", @style = "width:200px;", @maxlength = "50" })<span class="editTip"></span>
        </div>
        <div class="fitem">
            <label>供应商Token：</label>
            @Html.TextBoxFor(m => m.SuppToken, new { @class = "easyui-validatebox", @style = "width:200px;",  @maxlength = "50" })<span class="editTip"></span>
        </div>
        <div class="fitem">
            <label>联系人：</label>
            @Html.TextBoxFor(m => m.Contact, new { @class = "easyui-validatebox", @style = "width:200px;", @required = "true", @maxlength = "20" })
        </div>
        <div class="fitem">
            <label>手机号：</label>
            @Html.TextBoxFor(m => m.Mobile, new { @class = "easyui-validatebox", @style = "width:200px;", @required = "true", @maxlength = "11" })
        </div>         
        <div class="fitem">
            <label>
               供应商状态:
            </label>
            @Html.RadioButtonFor(m => m.SuppStatus, 1, new { @id = "radio1", @name = "SuppStatus" })启用
            @Html.RadioButtonFor(m => m.SuppStatus, 0, new { @id = "radio2", @name = "SuppStatus" })禁用
            <input type="hidden" name="SuppId" id="SuppId" />
        </div>
        <div class="fitem">
            <label style="color:red;font-weight:bold;">注意：</label><br />
            目前只有话费充值可以选择欧飞或聚通达，流量和Q币默认使用欧飞。聚通达接口只支持5，10，20，30，50，100，200，300，500面额话费。

        </div>
    </form>
</div>

